﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XmlHelpDef
{
    public class XmlHelperSelfDef
    {
        private string XmlPath="";
        private  XmlElement element=null; 
        private  XmlElement xmlNode=null;
        private string XmlfileName="";
        XmlDocument xmlDoc = null;
        List <XmlElement> listUnderNode=new List <XmlElement>();

       public XmlHelperSelfDef ()
        {

                XmlPath="";
                element=null; 
                xmlNode=null;
                XmlfileName="";
                xmlDoc = null;
        }
        
        public XmlElement AddElement(string nodeName)
        {
            XmlElement returnv = null;
            XmlElement xmlElement = xmlDoc.CreateElement(nodeName);
            element.AppendChild(xmlElement);
            returnv = xmlElement;
            return returnv;
        }
        public XmlElement AddElement(string lastnode, string nodeName)
        {
            XmlElement returnv = null;
            SetNode(lastnode);
            returnv = AddElement(nodeName);
            return returnv;
        }
          public bool SetNodeValue(string node, string innerText)
        {
            bool result;
            try
            {
                XmlElement xmlElement;
                if (!IsReadable())
                {
                    xmlElement = this.AddElement(node);
                }
                else
                {
                    xmlElement = (element.SelectSingleNode(node) as XmlElement);
                    if (xmlElement == null)
                    {
                        xmlElement = this.AddElement(node);
                    }
                }
                if (xmlElement != null)
                {
                    xmlElement.InnerText = innerText;
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
       
        public bool SetNodeValue(string lastnode, string node, string value)
        {
            bool result;
            try
            {
                this.SetNode(lastnode);
                result = this.SetNodeValue(node, value);
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
        public List <XmlAttribute> GetNodeAttributes (XmlElement node)
        {
            List <XmlAttribute>  list=new  List <XmlAttribute> ();
            foreach (XmlAttribute each in node.Attributes)
            {
                list.Add(each);
            }
            return list; 
        }      
        public bool IsReadable()
        {
            return this.xmlDoc != null && this.element != null && this.xmlNode != null;
        }

        public void SetNode(string xmlnode)
       {

            XmlNode xn=this.xmlNode.SelectSingleNode(xmlnode) ;
            XmlElement xe=xn as XmlElement;
            element = xe;
       }
        public string  GetAttribuateOfElement(string attribute)
        {
           string list = "";
           list = element.GetAttribute(attribute).ToString();
            return list;
        }
          public void   CreateXmlFile(string xmlFileName1)
         {
             if (System.IO.File.Exists(xmlFileName1))
            {
                  System.IO.File.Delete(xmlFileName1);
             }
            xmlDoc = new XmlDocument();
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
         //   this.m_xmlRootNode = this.m_xmlDoc.CreateElement("", rootNode, "");
          //   this.m_xmlDoc.AppendChild(this.m_xmlRootNode);
       //     elememt=null;
              xmlNode = (XmlElement)node;
   
         }
         public void   CreateXmlNode(string xmlnode)
         {
             if(xmlDoc!=null)
             {
                 XmlNode node = xmlDoc.CreateElement("", xmlnode, "");
                 xmlDoc.AppendChild(node);    
             }
         }
          public string GetXmlResultValue(string node)
        {
            string innerText;
            XmlNode xmlNode1 = xmlNode.SelectSingleNode(node);
            innerText = xmlNode1.InnerText;
            return innerText;
        }
      
        public string GetXmlResultValue(string lastnode, string curnode)
        {
            string innerText="";;
            try
            {
                this.SetNode(lastnode);
                innerText =GetXmlResultValue(curnode);
            }
            catch (Exception)
            {
                innerText = string.Empty;
            }
            return innerText;
        }
          public void SetAttribuateOfElement(string nodezf,string attribute)
          {

              element.SetAttribute(nodezf, attribute);
          }
          public bool LoadXmlFile(string xmlFileName1)
          {
              XmlfileName = xmlFileName1;

              xmlDoc = new XmlDocument();
           

              if (System.IO.File.Exists(xmlFileName1))
              {
                     xmlDoc.Load(xmlFileName1);
                     xmlNode = xmlDoc.DocumentElement;
                     element=xmlNode;
                     return true;
               }
               else
                  return false;
          }

          public bool SaveXml(string xmlFileName1)
          {
              if (xmlDoc == null || xmlNode == null || xmlFileName1 != XmlfileName)
              {
                 return false;
              }
              if (string.IsNullOrEmpty(xmlFileName1))
              {
                  return false;
              }
              xmlDoc.Save(xmlFileName1);
              return true;
          }
           public List<XmlAttribute> GetAttributes()
         {
            List<XmlAttribute> list = new List<XmlAttribute>();
            if (!this.IsReadable())
            {
                return list;
            }
            foreach (XmlAttribute xmlattri in element.Attributes)
            {
                list.Add(xmlattri);
            }
            return list;
        }
           public List<XmlAttribute> GetNodeAttributes(string node)
           {
               List<XmlAttribute> list = new List<XmlAttribute>();
               SetNode(node);

               list = this.GetAttributes();
               return list;
           }
           public List<XmlAttribute> GetCurrentNodeAttributes()
           {
               List<XmlAttribute> list = new List<XmlAttribute>();
               foreach (XmlAttribute xmlattri in element.Attributes)
               {

                   list.Add(xmlattri);
               }
               return list; 
              
           }
           public List<XmlElement> GetNodeElements()
           {
               List<XmlElement> list = new List<XmlElement>();
               XmlNodeList list2=element.ChildNodes;
               foreach (XmlNode node in list2)
               {
                   XmlElement e=node as XmlElement;
                   list.Add(e);
               }
               return list;
           }
            public List<XmlElement> GetNodeElements(string node)
        {
            List<XmlElement> list = new List<XmlElement>();
          
                SetNode(node);
                list = GetNodeElements();
                return list;
        }
            public static List<XmlElement> GetNodeElements(XmlElement lastnode)
            {
                List<XmlElement> list = new List<XmlElement>();
                XmlNodeList list2 = lastnode.ChildNodes;
                foreach (XmlNode xmlNode in lastnode)
                {
                    XmlElement e=xmlNode as XmlElement;
                    list.Add(e);
                }
                return list;
            }
    }
}
